package sealte.hvpms.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import sealte.hvpms.dao.impl.PollDAOImpl;
import sealte.hvpms.dao.impl.RuleDAOImpl;
import sealte.hvpms.dao.PollOptionDAO;
import sealte.hvpms.pojo.PollOption;
import sealte.hvpms.util.DBConnection;

public class PollOptionDAOImpl implements PollOptionDAO {
	private Connection conn = null;
	private PreparedStatement ps = null;
	private ResultSet rs = null;
	
	public boolean addNew(PollOption po){
		boolean flag = false;
		String sql = "insert into hvpms_poll_options(option_pollid,option_id,option_content,option_image,option_link) values(?,?,?,?,?)";
		try{
			conn = DBConnection.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, po.getPollid());
			ps.setInt(2, po.getId());
			ps.setString(3, po.getContent());
			ps.setString(4, po.getImage());
			ps.setString(5, po.getLink());
			ps.executeUpdate();
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.close(null, ps, conn);
		}
		return flag;
	}
	
	public List<PollOption> getPOAll(int poll_id){
		List<PollOption> all = new ArrayList<PollOption>();
		String sql = "select * from hvpms_poll_options where option_pollid=?";
		double all_polls = this.getAll_polls(poll_id);
		NumberFormat nf = NumberFormat.getPercentInstance();
		try{
			conn = DBConnection.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, poll_id);
			rs = ps.executeQuery();
			while(rs.next()){
				PollOption po = new PollOption();
				double counter = rs.getInt("option_counter");
				double percentage = counter/all_polls;
				po.setContent(rs.getString("option_content"));
				po.setCounter(rs.getInt("option_counter"));
				po.setId(rs.getInt("option_id"));
				po.setImage(rs.getString("option_image"));
				po.setLink(rs.getString("option_link"));
				po.setPercentage(nf.format(percentage));
				all.add(po);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.close(rs, ps, conn);
		}
		return all;
	}
	
	public boolean doVote(int poll_id,int[] options){
		boolean flag = false;
		String sql = "update hvpms_poll_options set option_counter=option_counter+1 where option_pollid=? and option_id=?";
		try{
			conn = DBConnection.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, poll_id);
			for(int i=0;i<options.length;i++){
				ps.setInt(2, options[i]);
				ps.executeUpdate();
			}
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.close(null, ps, conn);
		}
		return flag;
	}
	
	public int getAll_polls(int poll_id){
		int ap = 0;
		String sql = "select option_counter from hvpms_poll_options where option_pollid=?";
		try{
			conn = DBConnection.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, poll_id);
			rs = ps.executeQuery();
			while(rs.next()){
				ap += rs.getInt("option_counter");
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.close(rs, ps, conn);
		}
		return ap;
	}
	
	public boolean batchVPsBeZero(int[] poll_ids){
		boolean flag = false;
		RuleDAOImpl rb = new RuleDAOImpl();
		PollDAOImpl pb = new PollDAOImpl();
		int rule = 0;
		int poll_id = 0;
		String sql = "update hvpms_poll_options set option_counter=0 where option_pollid=?";
		try{
			conn = DBConnection.getConnection();
			ps = conn.prepareStatement(sql);
			for(int i=0;i<poll_ids.length;i++){
				poll_id = poll_ids[i];
				ps.setInt(1, poll_id);
				rule = pb.getRuleById(poll_id);
				if(rule==3){
					rb.deleteAllEmailByPollId(poll_id);
				}else if(rule==4){
					rb.deleteAllIPAddrByPollId(poll_id);
				}else if(rule==5){
					rb.deleteAllIDCardByPollId(poll_id);
				}
				ps.executeUpdate();
			}
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.close(null, ps, conn);
		}
		return flag;
	}
}
